package com.woniuxy.consumer;

import com.rabbitmq.client.Channel;
import com.woniuxy.entity.Order;
import com.woniuxy.mapper.OrderMapper;
import com.woniuxy.util.RabbitmqConst;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @author ：CuiYC
 * @description：
 * @date ：Created in 2025/3/20 16:09
 * @modified By：
 *根据订单编号
 * 获取订单状态
 * 判断是否是未付款状态
 *      是：改成取消
 *      否：不做处理
 */
@Component
public class CheckOrderStatConsumer {
    @Resource
    private OrderMapper orderMapper;
    @RabbitListener(queues = RabbitmqConst.CHECK_ORDER_STAT_QUEUE)
    public void receive(String msg, Channel channel, Message message) {
        try {
           //查询订单状态
            Order order = orderMapper.queryOrderStatByOrderNum(msg);
            if (order.getStatus() == 1){
                //修改订单状态
                order.setStatus(4);
                order.setOrderNum(msg);
                orderMapper.updateOrderStatus(order);
            }
            /**
             * 用户付款需要检查订单状态
             */

            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);

        } catch (Exception e) {
            e.printStackTrace();
            try {
                channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
            } catch (IOException ex) {
                ex.printStackTrace();
                throw new RuntimeException(ex);
            }
            throw new RuntimeException(e);
        }
    }
}
